import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;

import java.util.ArrayList;
import java.util.List;

//天猫订单分类统计
public class tmallTotal {
    public static void main(String[] args) {
        //第一步，打开Excel文件
        ExcelReader reader = ExcelUtil.getReader(FileUtil.file("e:/tm_out_ready.xlsx"));
        List<List<Object>> readAll = reader.read();
        reader.close();

        List<List<Object>> tmallTotal = new ArrayList<List<Object>>();
        List<Object> row = new ArrayList<Object>();

        for (int i = 0; i < readAll.size(); i++) {
            boolean isAdd = true;

            for (int j = 0; j < tmallTotal.size(); j++) {
                if (readAll.get(i).get(0).equals(tmallTotal.get(j).get(0)) && readAll.get(i).get(1).equals(tmallTotal.get(j).get(1))) {
                    isAdd = false;
                    Integer total = Convert.toInt(readAll.get(i).get(3)) + Convert.toInt(tmallTotal.get(j).get(3));
                    tmallTotal.get(j).set(3, total);
                }
            }

            if (isAdd) {
                List<Object> row2 = new ArrayList<Object>();
                row2.add(readAll.get(i).get(0));
                row2.add(readAll.get(i).get(1));
                row2.add(readAll.get(i).get(2));
                row2.add(readAll.get(i).get(3));
                tmallTotal.add(row2);
            }
        }

        //第三步，保存文件
        ExcelWriter writer = ExcelUtil.getWriter("e:/tm_out_total.xlsx");
        writer.write(tmallTotal);
        writer.close();
    }
}
